package tg.edu.fhh.demo.web.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import tg.edu.fhh.demo.entity.Course;
import tg.edu.fhh.demo.entity.Grade;
import tg.edu.fhh.demo.entity.Student;
import tg.edu.fhh.demo.entity.SysUser;
import tg.edu.fhh.demo.entity.TbClass;
import tg.edu.fhh.demo.security.UserUtils;
import tg.edu.fhh.demo.service.CourseService;
import tg.edu.fhh.demo.service.GradeService;
import tg.edu.fhh.demo.service.StudentService;
import tg.edu.fhh.demo.service.TbClassService;

@Controller
@RequestMapping(value = "/grade")
public class GradeController {
	
	@Autowired
	private GradeService gradeService;
	@Autowired
	private UserUtils userUtils;
	@Autowired
	private TbClassService tbclassService;
	@Autowired
	private StudentService studentService;
	@Autowired
	private CourseService courseService;

	//班级成绩查询，选择班级
	@RequestMapping(value = "/xzbj")
	public String xzbj(Integer bjId,ModelMap modelMap) {
		List<Course> courses=gradeService.findCourseByTbClassId(bjId);
		List<List<Object>> cjb= gradeService.getCjb(bjId,courses);
		modelMap.put("kcs", courses);
		modelMap.put("cjb", cjb);
		return "grade/bjcjcx01";
	}
	
	//班级成绩查询
	@RequestMapping(value = "/bjcjcx")
	public void bjcjcx(ModelMap modelMap) {
		List<TbClass> list=tbclassService.findAll();
		modelMap.put("list", list);
	}
	//班级成绩查询
	@RequestMapping(value = "/bjcjcx01 ")
	public String bjcjcx01(ModelMap modelMap) {
		SysUser user=userUtils.getUser();
		Integer id=user.getId();
		Student student=studentService.findById(id);
		Integer bjId=student.getTbClass().getId();
		List<Course> courses=gradeService.findCourseByTbClassId(bjId);
		List<List<Object>> cjb= gradeService.getCjb(bjId,courses);
		modelMap.put("kcs", courses);
		modelMap.put("cjb", cjb);
		modelMap.put("stu", student);
		return "grade/bjcjcx01";
	}
	//显示录入成绩表
	@RequestMapping(value = "/bjcjcx03")
	public String bjcjcx03(Integer bjId,Integer kcId,ModelMap modelMap) {
		List<Student> students=studentService.findByTbClassId(bjId);
		List<Grade> grades=new ArrayList<Grade>();
		for(Student s:students) {
			Grade grade=gradeService.findByStudentIdAndCourseId(s.getId(), kcId);
			if(grade==null) {
				Grade grade1=new Grade();
				grade1.setCourse(courseService.findById(kcId));
				grade1.setStudent(studentService.findById(s.getId()));
				gradeService.save(grade1);
				grades.add(grade1);
			}else {
				grades.add(grade);
			}
		}
		modelMap.put("grades", grades);
		return "grade/bjcjcx03";
		
	}
//班级成绩录入
	@RequestMapping(value = "/bjcjlr")
	public String bjcjlr(ModelMap modelMap) {
		List<TbClass> tbClasses=tbclassService.findAll();
		List<Course> courses=courseService.findAll();
		modelMap.put("bjs", tbClasses);
		modelMap.put("kcs", courses);
		return "grade/bjcjlr";
	}
	//修改成绩
	@RequestMapping(value = "/xgcj")
	@ResponseBody
	public void xgcj(Integer id,Integer grade) {
		Grade g=gradeService.findById(id);
		g.setGrade(grade);
		gradeService.save(g);
	}
	//个人成绩查询
	@RequestMapping(value = "/grcjcx")
	public String grcjcx(ModelMap modelMap) {
		SysUser user=userUtils.getUser();
		Integer id=user.getId();
		List<Grade> list=gradeService.findByStudentId(id);
		modelMap.put("list", list);
		return "grade/grcjcx";
	}
	//个人成绩录入
	@RequestMapping(value = "/personGradelr")
	public void personGradelr(ModelMap modelMap) {
		List<TbClass> tbClasses=tbclassService.findAll();
		modelMap.put("list", tbClasses);

	}
	//从班级获取学生名单
	@RequestMapping(value = "/getgrcj")
	public void getgrcj(Integer bjId,ModelMap modelMap) {
		List<Student> students=studentService.findByTbClassId(bjId);
		modelMap.put("list", students);
	}
	//得到学生成绩
	@RequestMapping(value = "/getgrcj01")
	public void getgrcj01(Integer stuId,ModelMap modelMap) {
		List<Grade> grades=gradeService.findByStudentId(stuId);
		modelMap.put("list", grades);
	}
	//修改成绩
	@RequestMapping(value = "/xgcj1")
	@ResponseBody
	public void xgcj1(Integer id,Integer grade) {
		Grade grade2=gradeService.findById(id);
		grade2.setGrade(grade);
		gradeService.save(grade2);
	}
}
